var KontorX = {
	ajaxIsSuccess: function(json) {
		var success = null;
		if (json.success != undefined) {
			success = json.success;
		} else
		if (json.response != undefined && json.response.success != undefined) {
			success = json.response.success;
		}
		return success == true ? true : false;
	}
};

$(function() {
	$("#mainmenu").lavaLamp({ fx: "backout", speed: 700 });
	
	$('.cart-add').livequery('click',function(){
		var self = this;
		$.getJSON(this.href ,{}, function(data){
			if (KontorX.ajaxIsSuccess(data)) {
				var href = self.href.replace('add','remove');
				$(self).removeClass('cart-add').addClass('cart-remove').attr('href', href).text('Usuń z koszyka');
				renderCart(data.cart);
			}
			return;
		});
		return false;
	});
	$('.cart-remove').livequery('click',function(){
		var self = this;
		$.getJSON(this.href ,{}, function(data){
			if (KontorX.ajaxIsSuccess(data)) {
				var href = self.href.replace('remove','add');
				$(self).removeClass('cart-remove').addClass('cart-add').attr('href', href).text('Dodaj do koszyka');
				renderCart(data.cart);
				$(self).parents('.cart-action-box').fadeOut('slow');
			}
			return;
		});
		return false;
	})
	
	var cartQuantity = $('.cart-quantity');
	var cartSumcost = $('.cart-sumcost');

	function renderCart(cart) {
		cartQuantity.text(0);
		cartSumcost.text(0);

		$.each(cart, function(i,e){
			cartQuantity.text(parseInt(cartQuantity.text()) + e.quantity);
			cartSumcost.text(parseInt(cartSumcost.text()) + parseInt(e.price));
		});
	}
});